home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / misc / savename.lha / SaveNames1.3 / SaveNames.c < prev    next >
C/C++ Source or Header  |  1995-09-30  |  14KB  |  509 lines

  1.  
  2. /**************************************************************************
  3.  *                                                                        *
  4.  *                            Save Names 1.3                              *
  5.  *                           ----------------                             *
  6.  *                                                                        *
  7.  *  SaveNames is freely distributable but is copyrighted by its author,   *
  8.  * Marcin Orlowski (carlos@felix.univ.szczecin.pl). For more information  *
  9.  * read the short documentation included in this archive. SaveNames is    *
  10.  * written in ANSI C, so may be compiled on any machine, except for pc    *
  11.  * (do you see any sense? :). Unfortunately small changes must be done    *
  12.  * befor you run compiler, so look below, and select your platform.       *
  13.  * AMIGA side was tested on A4000, UNIX side was tested on IRIS INDIGO,   *
  14.  * HP. The LINUX side was tested... on LINUX :-).                         *
  15.  *                                                                        *
  16.  * Use and enjoy.                                                         *
  17.  *                                                                        *
  18.  * Any suggestions, bugs, mails or anything else send to:                 *
  19.  *                                                                        *
  20.  * Marcin Orlowski                                                        *
  21.  * Radomska 38                                                            *
  22.  * 71-002 Szczecin                                                        *
  23.  * Poland                                                                 *
  24.  *                                                                        *
  25.  * or via net:                                                            *
  26.  *                                                                        *
  27.  * Internet: carlos@felix.univ.szczecin.pl                                *
  28.  * FidoNet:       Marcin Orlowski@2:481/22.2                              *
  29.  * GlobalNet:     Marcin Orlowski@52:4800/6                               *
  30.  *                                                                        *
  31.  * or try Silver Dream!'s BBS (non-stop) at +48 91 540431                 *
  32.  *                                                                        *
  33.  *************************************************************************/
  34.  
  35. /* Define your favourite platform (except Amiga :-) */
  36.  
  37. #define AMIGA
  38.  
  39. /*                     NOTE: LINUX automagically defines UNIX by itself,
  40.                                so you don't need to uncomment it by hand
  41. #define AMIGA
  42. #define UNIX
  43. #define LINUX
  44. */
  45.  
  46. /* Uncomment this only for compilers requiring C&R declaration type */
  47. /*
  48. #define OLD_STYLE
  49. */
  50.  
  51. /*************************************************************************/
  52.  
  53. #ifdef AMIGA
  54. char __stdiowin[] = "con:0/15/500/230/W.F.M.H. SaveNames 1.3";
  55. #define CURRENTDIR ""
  56. #include <sys/dir.h>
  57.  
  58. #ifndef EXIT_SUCCESS
  59. #define EXIT_SUCCESS 0
  60. #endif
  61. #ifndef EXIT_WARN
  62. #define EXIT_WARN 5
  63. #endif
  64. #ifndef EXIT_FAILURE
  65. #define EXIT_FAILURE 20
  66. #endif
  67.  
  68. /* Do not touch this if you don't know what you are doing! */
  69. /*
  70. #ifdef MAXNAMELEN
  71. #undef MAXNAMELEN
  72. #endif
  73. #define MAXNAMELEN 108
  74. */
  75. #endif
  76.  
  77. /*************************************************************************/
  78.  
  79. #ifdef LINUX
  80. #include <unistd.h>
  81. #ifndef UNIX
  82. #define UNIX
  83. #endif
  84. #endif
  85.  
  86. /*************************************************************************/
  87.  
  88. #ifdef UNIX
  89. #ifdef CURRENTDIR
  90. #undef CURRENTDIR
  91. #endif
  92. #define CURRENTDIR "."
  93. #define PARENTDIR ".."
  94. #include <dirent.h>
  95. #include <sys/types.h>
  96. #endif
  97.  
  98. /*************************************************************************/
  99.  
  100. #include <sys/stat.h>
  101. #include <fcntl.h>
  102.  
  103. #include <stdio.h>
  104. #include <stdlib.h>
  105. #include <string.h>
  106. #include <ctype.h>
  107.  
  108. #ifndef TRUE
  109. #define TRUE 1
  110. #endif
  111. #ifndef FALSE
  112. #define FALSE ~TRUE
  113. #endif
  114.  
  115. /*************************************************************************/
  116.  
  117. #define    AMI_DD_OFS  853000                /* Capacity of various floppy disks   */
  118. #define    AMI_DD_FFS  880000        /* This values may not fit exactly,   */
  119. #define    AMI_HD_FFS 1780000        /* but it doesn't matter so much...   */
  120. #define    PC_DD              725000                /* In case of big number of files,    */
  121. #define    PC_HD             1455000          /* the disk space is going down, 'cos */
  122.                                   /* of file headers and similar things */
  123.  
  124. #define    PCNAME    12                                 /* 8 + . + 3 */
  125.  
  126. FILE    *FileHandle = NULL;
  127. DIR    *dfd;                                            /* directory descriptor */
  128. int    fh;                                                /* FileHandle */
  129. struct    dirent    *dptr;                        /* dir entry */
  130. struct    stat        *st = NULL;                /* file information */
  131.  
  132. char    TempBuff[PCNAME+1];                /* +1 because of trailing NULL */
  133. char    CurrentDir[MAXNAMLEN+2];
  134.  
  135. struct    Entry    *FirstEntry= NULL;    /* for queue of entrires... */
  136. struct  Entry    *LastEntry = NULL;
  137. struct    Entry *TempEntry = NULL;
  138. struct  Entry *Temp2         = NULL;
  139.  
  140. struct    Entry                                            /* ... like this one...  */
  141. {
  142.     struct    Entry *Next;                        /* Successor             */
  143.     char        INode[PCNAME+1];                /* Almost unique file ID */
  144.     char        Name[MAXNAMLEN+1];            /* Oryginal file name    */
  145. };
  146.  
  147. int    Index;                                        /* Universal counter    */
  148. int    Count = 0, SkipCount = 0;    /* Almost universal counters :) */
  149. unsigned long TotalSize = 0;            /* Guess yourself... */
  150. char    ErrorHeader = TRUE;
  151. char    DecodeResult;
  152.  
  153. /*************************************************************************/
  154.  
  155. #ifdef OLD_STYLE
  156. void main(args, argv)
  157. int args;
  158. char **argv;
  159. #else
  160. void main(int args, char **argv)
  161. #endif
  162. {
  163.     printf("SaveNames 1.3 by Marcin Orlowski\n");
  164.     printf("E-Mail: carlos@felix.univ.szczecin.pl\n\n");
  165.  
  166.     if(args != 3)
  167.         {
  168. #ifdef AMIGA
  169.         if(args == 0)
  170.             printf("** Can't be run from Workbench.\n\n");
  171. #endif
  172.  
  173. #ifdef    UNIX
  174.         printf("Type 'man SaveNames'");
  175. #else
  176.         printf("Read file 'SaveNames.doc'");
  177. #endif
  178.  
  179.         printf(" to get whole documentation\n\n");
  180.  
  181.         if(strlen(argv[0]))
  182.             printf("Usage: %s ", argv[0]);
  183.         else
  184.             printf("Shell Usage: SaveNames ");
  185.  
  186.         printf("Command DirName\n");
  187.         printf("Command - type 'C'  or 'CODE' to crypt names\n");
  188.         printf("          type '-C' or '-COUNT' to count disks usage only\n");
  189.         printf("          type 'D'  or 'DECODE' to restore names\n");
  190.         printf("          type '-D' or '-DECODE' for expanded restore\n");
  191.         printf("DirName - process contents of this directory\n");
  192.  
  193.         exit(EXIT_FAILURE);
  194.         }
  195.  
  196.  
  197.     getcwd(CurrentDir, sizeof(CurrentDir));
  198.     if(chdir(argv[2]))
  199.         {
  200.         printf("** Can't find directory.\n");
  201.         exit(EXIT_FAILURE);
  202.         }
  203.  
  204.  
  205.  
  206.     if(argv[1][0] == 'C' || argv[1][0] == 'c' || (argv[1][0] == '-' && argv[1][1] == 'C') || (argv[1][0] == '-' && argv[1][1] == 'c'))
  207.         {
  208.         if((dfd = opendir(CURRENTDIR)))
  209.             {
  210.             char    running = TRUE;
  211.  
  212.                 FileHandle = NULL;
  213.                 if((FileHandle = fopen("WFMHFile.idx", "r")) == NULL)
  214.                     if((FileHandle = fopen("wfmhfile.idx", "r")) == NULL)
  215.                         FileHandle = fopen("WFMHFILE.IDX", "r");
  216.  
  217.             if(argv[1][0] == 'C' && argv[1][0] == 'c')
  218.                 {
  219.                 if(FileHandle)
  220.                     {
  221.                     fclose(FileHandle);
  222.                     printf("** Index file found in drawer '%s'.\n", argv[2]);
  223.                     printf("** It means you have already coded filenames.\n");
  224.                     printf("** Because this operation cannot be nested\n");
  225.                     printf("** it has been aborted. Watch your moves!\n");
  226.                     chdir(CurrentDir);
  227.                     exit(EXIT_FAILURE);
  228.                     }
  229.                 }
  230.  
  231.  
  232.             if(argv[1][0] == '-')
  233.                 printf("Counting floppy disks usage ...\n");
  234.             else
  235.                 printf("Coding filenames...\n");
  236.  
  237.             Count = 0;
  238.             if((st = malloc(sizeof(struct stat))) == NULL)
  239.                 printf(" ** No length counting. No memory for structure.\n");
  240.  
  241.                 while((dptr = readdir(dfd)) && running)
  242.                     {
  243. #ifdef UNIX
  244.                     if((strcmp(dptr->d_name, CURRENTDIR) != 0) && (strcmp(dptr->d_name, PARENTDIR) != 0))
  245.                             {
  246. #endif
  247.                         if((TempEntry = (struct Entry *)malloc(sizeof(struct Entry))))
  248.                             {
  249.                             strcpy(TempEntry->Name, dptr->d_name);
  250.                             sprintf(TempEntry->INode, "%lx.pcp", dptr->d_ino);
  251.  
  252.                             if(!(FirstEntry))
  253.                                 FirstEntry = TempEntry;
  254.                             else
  255.                                 LastEntry->Next = TempEntry;
  256.                             LastEntry = TempEntry;
  257.                             TempEntry->Next = NULL;
  258.  
  259.                             if(st)
  260.                                 if(fh = open(dptr->d_name, O_RDONLY, 0))
  261.                                     {
  262.                                     if(fstat(fh, st) == 0)
  263.                                         TotalSize += st->st_size;
  264.                                     close(fh);
  265.                                     }
  266.  
  267.                             Count++;
  268.                             }
  269.                         else
  270.                             {
  271.                             printf("** Out of memory. %d files scanned.\n", Count);
  272.                             printf("** Skipping the rest of files.\n");
  273.                             running = FALSE;
  274.                             }
  275. #ifdef UNIX
  276.                         }
  277. #endif
  278.  
  279.                     }
  280.  
  281.             closedir(dfd);
  282.  
  283.             if(st)
  284.                 {
  285.                 if(TotalSize != 0)
  286.                     {
  287.                     printf("\n Disks summary for %ld files (%ld bytes, %ld KB, %ld MB)\n\n", Count, TotalSize, (TotalSize/1024), (TotalSize/1048576));
  288.                     printf(" Disk type       Cnt  Free on last\n");
  289.                     printf(" ---------------------------------\n");
  290.                     printf(" Amiga DD (OFS) %3ld   %6ld KB\n", (AMI_DD_OFS + TotalSize) / AMI_DD_OFS, (AMI_DD_OFS * ((AMI_DD_OFS + TotalSize) / AMI_DD_OFS) - TotalSize)/1024);
  291.                     printf(" Amiga DD (FFS) %3ld   %6ld KB\n", (AMI_DD_FFS + TotalSize) / AMI_DD_FFS, (AMI_DD_FFS * ((AMI_DD_FFS + TotalSize) / AMI_DD_FFS) - TotalSize)/1024);
  292.                     printf(" Amiga HD (FFS) %3ld   %6ld KB\n", (AMI_HD_FFS + TotalSize) / AMI_HD_FFS, (AMI_HD_FFS * ((AMI_HD_FFS + TotalSize) / AMI_HD_FFS) - TotalSize)/1024);
  293.                     printf(" pc    DD       %3ld   %6ld KB\n", (PC_DD + TotalSize) / PC_DD, (PC_DD * ((PC_DD + TotalSize) / PC_DD) - TotalSize)/1024);
  294.                     printf(" pc    HD       %3ld   %6ld KB\n\n", (PC_HD + TotalSize) / PC_HD, (PC_HD * ((PC_HD + TotalSize) / PC_HD) - TotalSize)/1024);
  295.                     }
  296.                 else
  297.                     printf("No files - no summary :-(...\n");
  298.                 }
  299.     
  300.         if(st)
  301.                 {
  302.                 free(st);
  303.                 st = NULL;
  304.                 }
  305.  
  306.             if(argv[1][0] == '-')                                    /* We wanted summary only */
  307.                 {
  308.                 TempEntry = FirstEntry;
  309.                 while(TempEntry)
  310.                     {
  311.                     Temp2 = TempEntry->Next;
  312.                     free(TempEntry);
  313.                     TempEntry = Temp2;
  314.                     }
  315.                 chdir(CurrentDir);
  316.                 exit(EXIT_SUCCESS);
  317.                 }
  318.  
  319.             if(FileHandle = fopen("WFMHFile.idx", "w"))
  320.                 {
  321.                 Count = 0;
  322.                 TempEntry = FirstEntry;
  323.  
  324.                 while(TempEntry)
  325.                     {
  326.                     if(rename(TempEntry->Name, TempEntry->INode))
  327.                         {
  328. #ifdef UNIX
  329.                     if((strcmp(TempEntry->Name, CURRENTDIR) != 0) && (strcmp(TempEntry->Name, PARENTDIR) != 0))
  330.                             {
  331. #endif
  332.                             if(ErrorHeader == TRUE)
  333.                                 {
  334.                                 printf("I can't rename following files:\n");
  335.                                 printf("-------------------------------\n");
  336.                                 ErrorHeader = FALSE;
  337.                                 }
  338.                             printf("'%s'\n", TempEntry->Name);
  339.                             SkipCount++;
  340.                             }
  341. #ifdef UNIX
  342.                         }
  343. #endif
  344.                     else
  345.                         {
  346.                         fprintf(FileHandle, "%s%c%s\n", TempEntry->INode, 0x09, TempEntry->Name);
  347.                         Count++;
  348.                         }
  349.  
  350.                     Temp2 = TempEntry->Next;
  351.                     free(TempEntry);
  352.                     TempEntry = Temp2;
  353.                     }
  354.  
  355.                 fclose(FileHandle);
  356.                 if(Count)
  357.                     printf("%d files processed\n", Count);
  358.                 if(SkipCount)
  359.                     printf("%d files skipped\n", SkipCount);
  360.                 if(Count)
  361.                     printf("Done.\n");
  362.                 }
  363.             else
  364.                 {
  365.                 TempEntry = FirstEntry;
  366.                 while(TempEntry);
  367.                     {
  368.                     Temp2 = TempEntry->Next;                    
  369.                     free(TempEntry);
  370.                     TempEntry = Temp2;
  371.                     }
  372.                 printf("** Can't open index file to write 'WFMHFile.idx'.\n");
  373.                 exit(EXIT_FAILURE);
  374.                 }
  375.             }
  376.         else
  377.             {
  378.             printf("** Can't open directory '%s'.\n", argv[2]);
  379.             exit(EXIT_FAILURE);
  380.             }
  381.         }
  382.  
  383.  
  384. /*                             ########### DECODING PART BEGINS ##############   */
  385.  
  386.     else
  387.  
  388.         if(argv[1][0] == 'D' || argv[1][0] == 'd' || (argv[1][0] == '-' && argv[1][1] == 'D') || (argv[1][0] == '-' && argv[1][1] == 'd'))
  389.             {
  390.             if((TempEntry = malloc(sizeof(struct Entry))))
  391.                 {
  392.                 if((FileHandle = fopen("WFMHFile.idx", "r")) == NULL)
  393.                     if((FileHandle = fopen("wfmhfile.idx", "r")) == NULL)
  394.                         FileHandle = fopen("WFMHFILE.IDX", "r");
  395.  
  396.                 if(FileHandle)
  397.                     {
  398.                     if(argv[1][0] == '-')
  399.                     printf("Decoding filenames in expanded mode...\n");
  400.                         else
  401.                     printf("Decoding filenames...\n");
  402.  
  403.                     Count = 0;
  404.                     while(EOF != fscanf(FileHandle, "%s", TempEntry->INode))
  405.                         {
  406.  
  407.                         do
  408.                             TempEntry->Name[0] = getc(FileHandle);
  409.                         while((TempEntry->Name[0] != 0xa) && (TempEntry->Name[0] != 9));
  410.  
  411.                         for(Index=0; Index<MAXNAMLEN; Index++)
  412.                             {
  413.                             TempEntry->Name[Index] = fgetc(FileHandle);
  414.                             if(((TempEntry->Name[Index]) == 0xa) || (TempEntry->Name[Index] == EOF))
  415.                                     break;
  416.                             }
  417.                         TempEntry->Name[Index] = 0x00;
  418.  
  419.  
  420.                     if(rename(TempEntry->INode, TempEntry->Name))
  421.                         {
  422.                         DecodeResult = FALSE;
  423.  
  424.                         if(argv[1][0] == '-')
  425.                             {
  426.                             Index = 0;
  427.                             while(TempEntry->INode[Index] != '\0')
  428.                                 {
  429.                                 TempEntry->INode[Index] = tolower((int)TempEntry->INode[Index]);
  430.                                 Index++;
  431.                                 }
  432.                             if(rename(TempEntry->INode, TempEntry->Name))
  433.                                 {
  434.                                 Index = 0;
  435.                                 while(TempEntry->INode[Index] != '\0')
  436.                                     {
  437.                                     TempEntry->INode[Index] = toupper((int)TempEntry->INode[Index]);
  438.                                     Index++;
  439.                                     }
  440.                                 if(rename(TempEntry->INode, TempEntry->Name) == 0)
  441.                                     {
  442.                                     Count++;
  443.                                     DecodeResult = TRUE;
  444.                                     }
  445.                                 }
  446.                             }
  447.  
  448.                         if(DecodeResult == FALSE)
  449.                             {
  450.                             if(ErrorHeader == TRUE)
  451.                                 {
  452.                                 printf("I can't restore following files:\n");
  453.                                 printf("--------------------------------\n");
  454.                                 ErrorHeader = FALSE;
  455.                                 }
  456.                             printf("'%s'\n", TempEntry->Name);
  457.                             SkipCount++;
  458.                             }
  459.                         }
  460.                     else
  461.                         Count++;
  462.                         }
  463.  
  464.                     fclose(FileHandle);
  465.                     free(TempEntry);
  466.                     if(Count)
  467.                         printf("%d files restored\n", Count);
  468.                     if(SkipCount)
  469.                         printf("%d files skipped\n", SkipCount);
  470.                     if(Count)
  471.                         printf("Done.\n");
  472.  
  473.                     if(SkipCount == 0)
  474.                         {
  475.                         if(remove("WFMHFile.idx") != 0)
  476.                             if(remove("wfmhfile.idx") != 0)
  477.                                 if(remove("WFMHFILE.IDX") != 0)
  478.                                     {
  479.                                     printf("** Can't remove index file.\n");
  480. #ifdef AMIGA
  481.                                     exit(EXIT_WARN);
  482. #endif
  483.                                     }
  484.                         }
  485.                     }
  486.                 else
  487.                     {
  488.                     free(TempEntry);
  489.                     printf("** Can't find index file!\n");
  490.                     exit(EXIT_FAILURE);
  491.                     }
  492.                 }
  493.             else
  494.                 {
  495.                 printf("** Can't allocate work buffer!\n");
  496.                 exit(EXIT_FAILURE);
  497.                 }
  498.  
  499.             }
  500.         else
  501.             {
  502.             printf("** Unknown command.\n");
  503.             exit(EXIT_FAILURE);
  504.             }
  505.  
  506.     chdir(CurrentDir);
  507.     exit(EXIT_SUCCESS);
  508. }
  509.